home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume89 / iff / if2sixel.1 next >
Internet Message Format  |  1989-05-09  |  21KB

  1. Path: xanth!indri!ames!oliveb!sun!swap!page
  2. From: page%swap@Sun.COM (Bob Page)
  3. Newsgroups: comp.sources.amiga
  4. Subject: v89i120:  iff2sixel - convert iff files to dec sixel format
  5. Message-ID: <103774@sun.Eng.Sun.COM>
  6. Date: 9 May 89 05:00:30 GMT
  7. Sender: news@sun.Eng.Sun.COM
  8. Lines: 778
  9. Approved: page@sun.com
  10.  
  11. Submitted-by: kennedy%wwpacs@dupont.com (Tony Kennedy)
  12. Posting-number: Volume 89, Issue 120
  13. Archive-name: iff/iff2sixel.1
  14.  
  15. Here's a program I wrote to convert IFF pictures to DEC Sixels.  You
  16. can now display IFF files on VT240s, VT241s, VT340s and the like.
  17.  
  18. [binary not available; use your favorite compiler.  ..bob]
  19.  
  20. # This is a shell archive.
  21. # Remove anything above and including the cut line.
  22. # Then run the rest of the file through 'sh'.
  23. # Unpacked files will be owned by you and have default permissions.
  24. #----cut here-----cut here-----cut here-----cut here----#
  25. #!/bin/sh
  26. # shar: SHell ARchive
  27. # Run the following text through 'sh' to create:
  28. #    ReadMe
  29. #    iff2sixel.c
  30. # This is archive 1 of a 1-part kit.
  31. # This archive created: Mon May  8 21:54:31 1989
  32. echo "extracting ReadMe"
  33. sed 's/^X//' << \SHAR_EOF > ReadMe
  34. XHi there,
  35. X
  36. XHere's the program. It's got a few limitations. It NEEDS a 16 color
  37. Xfile for a 340 (you can use fewer that 16 but not more), or 4 colors
  38. Xfor a 240.  I ran it with HAM converted to hires interlace with
  39. XPixmate and they look great.  The best results are the 16 color hires
  40. Xinterlaced picts.
  41. X
  42. XLet me know if you find this program useful and if there are any bugs.
  43. XAlso please let me know what environment/compiler/linker you used (for
  44. Xfuture versions)
  45. X
  46. X            Enjoy...
  47. X                ...Tony
  48. SHAR_EOF
  49. echo "extracting iff2sixel.c"
  50. sed 's/^X//' << \SHAR_EOF > iff2sixel.c
  51. X/*****************************************************************************/
  52. X/*                                         
  53. X    Program  : IFF2SIXEL.C
  54. X
  55. X    Author   : Tony Kennedy
  56. X           Internet address: kennedam%wwps@dupont.com@relay.cs.net
  57. X
  58. X    Purpose  : This program takes a AMIGA IFF ILBM file and converts
  59. X           it to a SIXEL file to be displayed on DIGITAL EQUIPMENT
  60. X           CORP. VT240, VT241, VT340 terminals. 
  61. X                                 
  62. X           This version will handle only 16 colors for a VT340 and
  63. X           4 colors for a VT240. It does no color compression, so
  64. X           all input files must be 16 colors or less for a 340,
  65. X           4 colors or less for a 240. 
  66. X
  67. X           If the display is a VT240/241 then the resolution
  68. X           is set to 640X200. If it's a 340 then the res is 640X400.
  69. X
  70. X           I tried to make it a generic as possible so you can 
  71. X           compile it under what ever environment you please.
  72. X           If you do it under VMS you will need to define IFF2SIXEL
  73. X           as a foreign command i.e.  $ iff2sixel :== $mydir:iff2sixel
  74. X           otherwise you can't use command line args.
  75. X
  76. X    Usage    : iff2sixel <input_file> <output_file> <VT240 | VT340>
  77. X
  78. X    Comments : This program is Freely Distributable. You may
  79. X           use it for any non-commercial uses.  
  80. X                   (If anybody hacks at this please send me a copy.)
  81. X
  82. X    Acknowledgements : Thanks to...
  83. X
  84. X                 Carter Brock for the SHOW_IFF program I used
  85. X                to figure out how to read an IFF file.
  86. X
  87. X                Mark Thompson and Steve Berry for the IFF2SUN
  88. X                program I used to figure out how to interpret
  89. X                the bitplanes.
  90. X
  91. X                Barry Young for his help with SIXEL formats.
  92. X
  93. X                Daniel Jay Barrett for his switching bytes
  94. X                hacks.
  95. X
  96. X    Limitations : This won't do HAM or Halfbrite. When I figure them out
  97. X              I'll put that in too.
  98. X              Won't do any color compression.
  99. X
  100. X
  101. X    Versions:        Revisions : 
  102. X    
  103. X    1.0  3/1/89         Original program.
  104. X
  105. X*/
  106. X/*****************************************************************************/
  107. X
  108. X#include <stdio.h>
  109. X#include <math.h>
  110. X
  111. X#define Sixel_base 63
  112. X#define DCS        144
  113. X#define ST         156
  114. X#define QUOTE      34
  115. X
  116. X#define LoRes  1
  117. X#define LoLace 2
  118. X#define HiRes  3
  119. X#define HiLace 4
  120. X
  121. X#define VT240_LoRes  1
  122. X#define VT240_LoLace 2
  123. X#define VT240_HiRes  3
  124. X#define VT240_HiLace 4
  125. X#define VT340_LoRes  5
  126. X#define VT340_LoLace 6
  127. X#define VT340_HiRes  7
  128. X#define VT340_HiLace 8
  129. X
  130. X typedef unsigned char ID_type[4];
  131. X struct ChunkType
  132. X    {
  133. X        ID_type Ident ;
  134. X        long    ChunkSize ;
  135. X    } ;
  136. X struct ChunkType chunk ;
  137. X
  138. X typedef struct 
  139. X    {
  140. X        unsigned char red, green, blue;
  141. X    } ColorRegType;                   
  142. X
  143. X unsigned char BitPlaneArray[400][6][128]; /* vert posit, plane, width posit*/  
  144. X unsigned char PixelArray[400][640];      /* vert posit, horiz posit */
  145. X int           SixelLine[16][640][80];   /* color,horizontal posit, row.*/
  146. X int           vert, horz, s_vert, v_bit;
  147. X
  148. X/********************************************************************
  149. X
  150. X    This function decompresses the IFF file. I got the algorithm
  151. X    from Mark and Steve who got it from Leo (The Great Caped One).
  152. X
  153. X********************************************************************/
  154. X
  155. X int decompress_planes(nplanes,width,height,inp_file)
  156. X
  157. X unsigned short nplanes,width,height;
  158. X FILE *inp_file;
  159. X {
  160. X   long h,p,count,BytesPerRow;
  161. X   char len;
  162. X   unsigned char byte;
  163. X
  164. X   for (h=0;h<height;h++)
  165. X     {
  166. X    for (p=0;p<nplanes;p++)
  167. X      {
  168. X         count = 0;
  169. X         BytesPerRow=width/8;
  170. X         while(BytesPerRow>0)
  171. X        {
  172. X          if ((len = getc(inp_file)) >= 0)
  173. X             {
  174. X            BytesPerRow -= ++len;
  175. X            fread(&BitPlaneArray[h][p][count],len,1,inp_file);
  176. X            count += len;
  177. X             }
  178. X          else 
  179. X             if (len < 0)
  180. X             {
  181. X            len = -len + 1;
  182. X            BytesPerRow -= len;
  183. X            byte = getc(inp_file);
  184. X            while (--len >= 0)
  185. X                BitPlaneArray[h][p][count++] = byte;
  186. X             }
  187. X        }
  188. X         if (BytesPerRow) printf("Compression is corrupt.\n");
  189. X    }
  190. X     }
  191. X  }
  192. X
  193. X/********************************************************************
  194. X
  195. X    Since the VAX reads shorts and longs backwards we've got to 
  196. X    flip them around...
  197. X
  198. X********************************************************************/
  199. X
  200. X#ifdef VAX
  201. X
  202. Xshort flip_byte_short(s)
  203. X
  204. X  short s;
  205. X  {
  206. X    short hib,lob;
  207. X
  208. X    lob = (s & 255);
  209. X    hib = ((s >> 8) & 255);
  210. X    return ((lob << 8) + hib);
  211. X  }
  212. X
  213. Xlong flip_bytes_long(l)
  214. X
  215. X  long l;
  216. X  {
  217. X    short low,hiw;
  218. X
  219. X    low = (l & 65535);
  220. X    hiw = ((l >> 16) & 65535);
  221. X    return ( ((flip_byte_short(low) << 16) + flip_byte_short(hiw)));
  222. X  }
  223. X#endif
  224. X
  225. X/********************************************************************
  226. X
  227. X    Convert a string to upper case.
  228. X
  229. X********************************************************************/
  230. X
  231. Xchar *upcase(str)
  232. X  char *str;
  233. X
  234. X  {
  235. X    int len, i;
  236. X
  237. X    i=0;
  238. X    len=strlen(str);
  239. X    while(i != len)
  240. X        {
  241. X          str[i]=toupper(str[i]);
  242. X          i++;
  243. X        }
  244. X    return str;
  245. X  }
  246. X
  247. X/********************************************************************
  248. X
  249. X    Double the width of the Pixel Array.
  250. X
  251. X********************************************************************/
  252. X
  253. X short double_width(width,height)
  254. X
  255. X  short width,height;
  256. X
  257. X  {
  258. X    short h,w;
  259. X    for (h=0;h<height;h++)
  260. X      {
  261. X        for (w=width-1;w>=0;w--)
  262. X        {
  263. X          PixelArray[h][w * 2] = PixelArray[h][w];
  264. X          PixelArray[h][(w * 2)+1] = PixelArray[h][w];
  265. X        }                       
  266. X      }
  267. X      return (width * 2);
  268. X  }
  269. X
  270. X/********************************************************************
  271. X
  272. X    Double the height of the Pixel array.
  273. X
  274. X********************************************************************/
  275. X
  276. X short double_height(width,height)
  277. X
  278. X  short width,height;
  279. X
  280. X  {
  281. X    short h,w;
  282. X    for (w=0;w<width;w++)
  283. X      for (h=height-1;h>=0;h--)
  284. X        {
  285. X          PixelArray[h * 2][w]=PixelArray[h][w];
  286. X          PixelArray[(h * 2)+1][w]=PixelArray[h][w];
  287. X        }
  288. X    return (height * 2);
  289. X  }
  290. X
  291. X/********************************************************************
  292. X
  293. X    Halve the height of the Pixel Array. This is gonna make the
  294. X    picture look pretty ugly if you ask me. Only gets done if a
  295. X    picture for a VT240 from an interlace IFF file is being 
  296. X    generated.
  297. X
  298. X********************************************************************/
  299. X
  300. Xshort halve_height(width,height)
  301. X
  302. X  short width,height;
  303. X
  304. X  {
  305. X    short h,w;
  306. X    for (h=0;h<height;h++)
  307. X        for (w=0;w<=width;w++)
  308. X            PixelArray[h][w]=PixelArray[(h * 2)][w];
  309. X
  310. X    return (height/2);
  311. X  }                                            
  312. Xmain (argc,argv)
  313. X
  314. X int argc;
  315. X char **argv[];
  316. X {
  317. X  long num_colors;
  318. X  int stat;
  319. X  long posit,h,w,p,i,j,k,l,m,pixel;
  320. X  long w_pos,cnt_100,cnt_10,cnt_1,rep_cnt;
  321. X  char ch, outline[700];
  322. X  unsigned char  color;
  323. X  char *term;
  324. X  char term_type[5];
  325. X  char *input_file;
  326. X  char *output_file;
  327. X  FILE *outf,*inf;
  328. X  char start_sixel = DCS;
  329. X  char end_sixel = ST;
  330. X  char quote = QUOTE;
  331. X  int  convert_mode = 0;
  332. X  int  resolution;
  333. X
  334. X  struct BitMapHeaderType
  335. X    {
  336. X        unsigned short width, height;
  337. X        short          org_x, org_y;
  338. X        unsigned char  NumPlanes, Mask, Compression, pad;
  339. X        unsigned short TransColor;
  340. X        unsigned char  X_Asp, Y_Asp;
  341. X        unsigned short PageWid, PageHeight;
  342. X    } ;
  343. X  struct BitMapHeaderType bmhd;
  344. X                              
  345. X  ColorRegType CMap[16];
  346. X  
  347. X/*********************************************************************
  348. X
  349. X First, get all the needed info from the user...            
  350. X              
  351. X    Input file, Output file, Type of terminal to create a file for.
  352. X
  353. X*********************************************************************/
  354. X  switch (argc)
  355. X    {
  356. X    case 1 :
  357. X        printf("Input File Name : ");
  358. X        scanf ("%s",&input_file);
  359. X        printf("Output File Name : ");
  360. X        scanf("%s",&output_file);    
  361. X        printf("\nThis will generate SIXELS for a VT240 @ 640x240.\n");
  362. X        printf("or a VT340 @ 640x480.\n");    
  363. X        printf("Enter VT240 or VT340 : ");
  364. X        scanf("%s",&term_type);
  365. X        inf = fopen(&input_file,"r");
  366. X        outf = fopen(&output_file,"w");
  367. X        term = &term_type;
  368. X        break;                   
  369. X        
  370. X    case 2 :
  371. X        
  372. X        printf("Output File Name : ");
  373. X        scanf("%s",&output_file);    
  374. X        printf("\nThis will generate SIXELS for a VT240 @ 640x240.\n");
  375. X        printf("or a VT340 @ 640x480.\n");    
  376. X        printf("Enter VT240 or VT340 : ");
  377. X        scanf("%s",&term_type);
  378. X        inf = fopen(argv[1],"r");
  379. X        outf = fopen(&output_file,"w");
  380. X        term = &term_type;
  381. X        break;             
  382. X
  383. X    case 3 :
  384. X                                     
  385. X        printf("\nThis will generate SIXELS for a VT240 @ 640x240.\n");
  386. X        printf("or a VT340 @ 640x480.\n");    
  387. X        printf("Enter VT240 or VT340 : ");
  388. X        scanf("%s",&term_type);
  389. X        inf = fopen(argv[1],"r");
  390. X        outf = fopen(argv[2],"w");
  391. X        term = &term_type;
  392. X        break;              
  393. X
  394. X    case 4 :
  395. X        
  396. X        inf = fopen(argv[1],"r");
  397. X        outf = fopen(argv[2],"w");
  398. X        term = argv[3];
  399. X                   
  400. X    }
  401. X
  402. X    if (outf == NULL) {
  403. X      printf("Output can't be opened.\n");
  404. X      exit(0); }
  405. X    
  406. X    if (inf == NULL) {
  407. X      printf("Input file can't be opened.\n");
  408. X      exit(0); }
  409. X
  410. X
  411. X/*********************************************************************
  412. X
  413. X    Ok. Now read in the IFF file...
  414. X
  415. X*********************************************************************/
  416. X
  417. X    
  418. X    stat=fread(chunk.Ident,4,1,inf);
  419. X    if (stat == -1) 
  420. X        {
  421. X            printf("Error reading file.\n");
  422. X            stat=close(inf) ; stat=close(outf);
  423. X            exit(0);
  424. X                }
  425. X                                     
  426. X
  427. X    while (strncmp(chunk.Ident,"FORM",4) !=0)  
  428. X        stat=fread(chunk.Ident,4,1,inf);
  429. X                                 
  430. X    if (strncmp(chunk.Ident,"FORM",4) != 0)
  431. X        {
  432. X            printf("Error, not a FORM file.\n");
  433. X            stat=close(inf); stat=close(outf);
  434. X            exit(0);
  435. X        }
  436. X
  437. X    stat=fread(&chunk.ChunkSize,4,1,inf);
  438. X
  439. X    stat=fread(chunk.Ident,4,1,inf);
  440. X                                               
  441. X    while (strncmp(chunk.Ident,"ILBM",4) !=0)  
  442. X        stat=fread(chunk.Ident,4,1,inf);
  443. X                                 
  444. X    if (strncmp(chunk.Ident, "ILBM",4) !=0)
  445. X                                               
  446. X        {
  447. X            printf("Error Not an ILBM file.\n");
  448. X                  stat=close(inf); stat=close(outf);
  449. X            exit(0);
  450. X        }                              
  451. X    stat=fread(chunk.Ident,4,1,inf);
  452. X        
  453. X    while (strncmp(chunk.Ident,"BMHD",4) !=0)  
  454. X        stat=fread(chunk.Ident,4,1,inf);
  455. X                                 
  456. X    if (strncmp(chunk.Ident, "BMHD",4) !=0)
  457. X        {
  458. X            printf("Error can't find Bit Map Header.\n");
  459. X                  stat=close(inf); stat=close(outf);
  460. X            exit(0);
  461. X        }                              
  462. X
  463. X    stat=fread(&chunk.ChunkSize,4,1,inf);
  464. X    stat=fread(chunk.Ident,2,1,inf);
  465. X    bmhd.width = *((short *)chunk.Ident);
  466. X    stat=fread(chunk.Ident,2,1,inf);
  467. X    bmhd.height = *((short *)chunk.Ident);
  468. X    stat=fread(chunk.Ident,2,1,inf);
  469. X    bmhd.org_x = *((short *)chunk.Ident);
  470. X    stat=fread(chunk.Ident,2,1,inf);
  471. X    bmhd.org_y = *((short *)chunk.Ident);
  472. X    stat=fread(chunk.Ident,1,1,inf);
  473. X    bmhd.NumPlanes = *((char *)chunk.Ident);
  474. X    stat=fread(chunk.Ident,1,1,inf);
  475. X    bmhd.Mask = *((char *)chunk.Ident);
  476. X    stat=fread(chunk.Ident,1,1,inf);
  477. X    bmhd.Compression = *((char *)chunk.Ident);
  478. X    stat=fread(chunk.Ident,1,1,inf);
  479. X    stat=fread(chunk.Ident,2,1,inf);
  480. X    bmhd.TransColor = *((short *)chunk.Ident);
  481. X    stat=fread(chunk.Ident,1,1,inf);
  482. X    bmhd.X_Asp = *((char *)chunk.Ident);
  483. X    stat=fread(chunk.Ident,1,1,inf);
  484. X    bmhd.Y_Asp = *((char *)chunk.Ident);
  485. X    stat=fread(chunk.Ident,2,1,inf);
  486. X    bmhd.PageWid = *((short *)chunk.Ident);
  487. X    stat=fread(chunk.Ident,2,1,inf);
  488. X    bmhd.PageHeight = *((short *)chunk.Ident);
  489. X
  490. X    if (stat == -1)
  491. X        {
  492. X            printf("Error failed to get Bit Map information.\n");
  493. X                  stat=close(inf); stat=close(outf);
  494. X            exit(0);
  495. X        }
  496. X
  497. X
  498. X    stat=fread(chunk.Ident,4,1,inf);
  499. X        
  500. X    while (strncmp(chunk.Ident,"CMAP",4) !=0)  
  501. X        stat=fread(chunk.Ident,4,1,inf);
  502. X                                 
  503. X    if (strncmp(chunk.Ident,"CMAP",4) !=0)
  504. X        {
  505. X            printf("Error can't find Color Map.\n");
  506. X                  stat=close(inf); stat=close(outf);
  507. X            exit(0);
  508. X        }                 
  509. X        stat=fread(&num_colors,4,1,inf);           
  510. X
  511. X#ifdef VAX
  512. X
  513. X    bmhd.width=flip_byte_short(bmhd.width);
  514. X    bmhd.height=flip_byte_short(bmhd.height);
  515. X    bmhd.org_x=flip_byte_short(bmhd.org_x);
  516. X    bmhd.org_y=flip_byte_short(bmhd.org_y);
  517. X    bmhd.TransColor=flip_byte_short(bmhd.TransColor);
  518. X    bmhd.PageWid=flip_byte_short(bmhd.PageWid);
  519. X    bmhd.PageHeight=flip_byte_short(bmhd.PageHeight);
  520. X    num_colors=flip_bytes_long(num_colors);
  521. X
  522. X#endif
  523. X
  524. X/********************************************************************
  525. X
  526. X    define conversion mode
  527. X
  528. X********************************************************************/
  529. X
  530. X    if ((bmhd.width==320)&&(bmhd.height==200)) resolution = LoRes;
  531. X    if ((bmhd.width==320)&&(bmhd.height==400)) resolution = LoLace;
  532. X    if ((bmhd.width==640)&&(bmhd.height==200)) resolution = HiRes;
  533. X    if ((bmhd.width==640)&&(bmhd.height==400)) resolution = HiLace;
  534. X
  535. X                                                             
  536. X    if  ((strcmp(upcase(term),"VT240",5) == 0) &&  
  537. X         (resolution==LoRes)) convert_mode=VT240_LoRes;
  538. X
  539. X    if  ((strcmp(upcase(term),"VT240",5) == 0) &&  
  540. X         (resolution==LoLace)) convert_mode=VT240_LoLace;
  541. X
  542. X    if  ((strcmp(upcase(term),"VT240",5) == 0) &&  
  543. X         (resolution==HiRes)) convert_mode=VT240_HiRes;
  544. X
  545. X    if  ((strcmp(upcase(term),"VT240",5) == 0) &&  
  546. X         (resolution==HiLace)) convert_mode=VT240_HiLace;
  547. X
  548. X    if  ((strcmp(upcase(term),"VT340",5) == 0) &&  
  549. X         (resolution==LoRes)) convert_mode=VT340_LoRes;
  550. X
  551. X    if  ((strcmp(upcase(term),"VT340",5) == 0) &&  
  552. X         (resolution==LoLace)) convert_mode=VT340_LoLace;
  553. X
  554. X    if  ((strcmp(upcase(term),"VT340",5) == 0) &&  
  555. X         (resolution==HiRes)) convert_mode=VT340_HiRes;
  556. X
  557. X    if  ((strcmp(upcase(term),"VT340",5) == 0) &&  
  558. X         (resolution==HiLace)) convert_mode=VT340_HiLace;
  559. X
  560. X    if (!convert_mode) {
  561. X        printf("IFF picture is an irregular size.\n");
  562. X        close(inf);
  563. X        close(outf);
  564. X        exit(0);
  565. X               }
  566. X
  567. X
  568. X    num_colors=num_colors/3;                         
  569. X    stat=fread(CMap,(num_colors * 3),1,inf);
  570. X                
  571. X        if ( (num_colors > 16) ||
  572. X         ((num_colors > 4) && (strcmp(upcase(term),"VT240",5) == 0))
  573. X       )
  574. X
  575. X        {
  576. X           printf("File has too many colors for this conversion.\n");
  577. X           close(inf);
  578. X           close(outf);
  579. X           exit(0);
  580. X        }
  581. X
  582. X/********************************************************************
  583. X
  584. X    Skip everything till we find the BODY.
  585. X
  586. X********************************************************************/
  587. X                                         
  588. X    stat=fread(chunk.Ident,4,1,inf); 
  589. X    while (strncmp(chunk.Ident,"BODY",4) !=0)  
  590. X        stat=fread(chunk.Ident,4,1,inf);
  591. X                                 
  592. X
  593. X    stat=fread(chunk.Ident,4,1,inf); /* body length */
  594. X
  595. X
  596. X/********************************************************************
  597. X
  598. X    Now read the bit planes in.
  599. X
  600. X********************************************************************/
  601. X
  602. X    if (bmhd.Compression == 1)
  603. X        decompress_planes(bmhd.NumPlanes,bmhd.width,bmhd.height,inf);
  604. X    else
  605. X
  606. X        for (h = 0;h<bmhd.height;h++)
  607. X            for (p = 0;p<bmhd.NumPlanes;p++) 
  608. X                {
  609. X                 for (w = 0;w<bmhd.width/8;w++) 
  610. X                    {
  611. X                     if (feof(inf)) continue;
  612. X                     read(inf,BitPlaneArray[h][p][w],1);
  613. X                            }
  614. X                                }
  615. X    close(inf);
  616. X
  617. X/********************************************************************
  618. X
  619. X    Translate bit planes to pixel color values.
  620. X
  621. X********************************************************************/
  622. X
  623. X        for (h = 0;h<bmhd.height;h++)
  624. X     {
  625. X     w_pos = 0;
  626. X     for (w=0;w<bmhd.width/8;w++)
  627. X       {                                                   
  628. X        pixel = 0;    
  629. X        for (i=7;i>=0;i--)
  630. X          {
  631. X           
  632. X        for (p=0;p<bmhd.NumPlanes;p++)
  633. X          pixel = ((((1 << i) & BitPlaneArray[h][p][w])>>i)<<p)|pixel;
  634. X           
  635. X        PixelArray[h][w_pos++] = (char)(pixel & 0x1f); 
  636. X        pixel = 0;
  637. X          }                                              
  638. X       }
  639. X     }    
  640. X
  641. X/********************************************************************
  642. X
  643. X    Convert to screen size.
  644. X
  645. X********************************************************************/
  646. X
  647. X    switch(convert_mode)
  648. X
  649. X        {
  650. X             case VT240_LoRes : 
  651. X              bmhd.width = double_width(bmhd.width,bmhd.height);
  652. X                          break;                        
  653. X
  654. X                 case VT240_LoLace :
  655. X              bmhd.height = halve_height(bmhd.width,bmhd.height);
  656. X              bmhd.width = double_width(bmhd.width,bmhd.height);
  657. X              break;
  658. X             
  659. X             case VT240_HiRes : 
  660. X              break;
  661. X                      
  662. X             case VT240_HiLace :
  663. X              bmhd.height = halve_height(bmhd.width,bmhd.height);
  664. X              break;
  665. X
  666. X             case VT340_LoRes :
  667. X              bmhd.width = double_width(bmhd.width,bmhd.height);
  668. X              bmhd.height = double_height(bmhd.width,bmhd.height);
  669. X              break;
  670. X
  671. X             case VT340_LoLace :
  672. X              bmhd.width = double_width(bmhd.width,bmhd.height);
  673. X                          break;                        
  674. X
  675. X             case VT340_HiRes :
  676. X              bmhd.height = double_height(bmhd.width,bmhd.height);
  677. X              break;
  678. X
  679. X             case VT340_HiLace :
  680. X              break;
  681. X
  682. X        }
  683. X                
  684. X/********************************************************************
  685. X
  686. X    Convert Pixels to Sixels.
  687. X
  688. X********************************************************************/
  689. X
  690. X    vert=bmhd.height;
  691. X    horz=bmhd.width;
  692. X    v_bit = 0;
  693. X                                
  694. X    for (i=0;i<vert;i++)
  695. X       {
  696. X        s_vert = i/6;
  697. X        v_bit= i%6;
  698. X        for (j=0;j<horz;j++)
  699. X           {
  700. X            color = PixelArray[i][j];                               
  701. X            SixelLine[color][j][s_vert] |= (int)pow(2,v_bit);
  702. X           }      
  703. X       }
  704. X                        
  705. X                                                   
  706. X/********************************************************************
  707. X
  708. X    Compress Sixels and write to file.
  709. X
  710. X********************************************************************/
  711. X
  712. X    
  713. X    vert = bmhd.height/6;
  714. X
  715. X/*    Write Sixel start code. */
  716. X
  717. X    fprintf(outf,"%c;1;;q%c1;1;;\n",start_sixel,quote);
  718. X
  719. X/*    Write color map. */
  720. X
  721. X    for (color=0;color<num_colors;color++)
  722. X      {
  723. X        CMap[color].red   = (CMap[color].red * 100)/255;        
  724. X        CMap[color].green = (CMap[color].green * 100)/255;
  725. X        CMap[color].blue  = (CMap[color].blue * 100)/255;
  726. X        fprintf(outf,"#%d;2;%d;%d;%d\n",color,
  727. X                CMap[color].red,CMap[color].green,CMap[color].blue);
  728. X
  729. X      }
  730. X      fprintf(outf,"\n");
  731. X
  732. X
  733. X/*     Compress and write Sixels. */
  734. X
  735. X    for (s_vert=0;s_vert <= vert;s_vert++)
  736. X     {
  737. X       for (color=0;color<num_colors;color++)
  738. X        {
  739. X
  740. X          for (j=0;j<horz;j++)                                
  741. X            SixelLine[color][j][s_vert] += Sixel_base;
  742. X          i = 0;
  743. X          k = 0;
  744. X          while( i < horz )
  745. X        {
  746. X           ch = SixelLine[color][i][s_vert];            
  747. X           l=i;
  748. X
  749. X           while ((l < horz) && (ch == SixelLine[color][l][s_vert]))
  750. X            l++;
  751. X           rep_cnt = l - i;
  752. X           if (rep_cnt >= 5)
  753. X            {
  754. X               cnt_100 = rep_cnt/100;
  755. X               cnt_10  = (rep_cnt-(cnt_100 * 100))/10;
  756. X               cnt_1   = (rep_cnt-((cnt_100 * 100)+(cnt_10 * 10)));
  757. X               outline[k] = 33; /* Exclamation mark */
  758. X               outline[k+1] = cnt_100 + 48;
  759. X               outline[k+2] = cnt_10  + 48;
  760. X               outline[k+3] = cnt_1   + 48;
  761. X               outline[k+4] = SixelLine[color][l-1][s_vert];
  762. X               k += 5;
  763. X               i=l;
  764. X            }
  765. X           else
  766. X            {
  767. X               for (m=0;m<=(l-1)-i;m++)
  768. X                   outline[k+m]=SixelLine[color][l-1][s_vert];
  769. X               k += (l-i);
  770. X               i=l;
  771. X            }
  772. X        }         
  773. X        outline[k]=0;
  774. X        fprintf(outf,"#%d %s$\n",color,&outline); 
  775. X
  776. X        }
  777. X        fprintf(outf," - \n");
  778. X    }
  779. X    fprintf(outf,"%c\n",ST);
  780. X    close(outf);
  781. X     }
  782. SHAR_EOF
  783. echo "End of archive 1 (of 1)"
  784. # if you want to concatenate archives, remove anything after this line
  785. exit
  786.